package com.ddxcloud.movie.controller;

import com.alipay.api.AlipayApiException;
import com.ddxcloud.common.constants.PayMethodConstant;
import com.ddxcloud.common.handler.BusinessException;
import com.ddxcloud.common.utils.Result;
import com.ddxcloud.movie.dto.PayDTO;
import com.ddxcloud.movie.service.AlipayService;
import com.ddxcloud.movie.service.WechatPayService;
import com.github.binarywang.wxpay.exception.WxPayException;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/pay")
@Api(tags = "支付")
public class PayController {

    @Resource
    WechatPayService wechatPayService;

    @Resource
    AlipayService alipayService;

    /**
     * 微信支付-统一下单
     * @param dto
     * @param bindingResult
     * @param request
     * @return
     */
    @Operation(summary = "微信支付-统一下单")
    @PostMapping("/createOrder")
    public Result<Object> createOrder(
            @Validated @RequestBody PayDTO dto,
            BindingResult bindingResult, HttpServletRequest request
    ) {
        if (bindingResult.hasErrors()) {
            String errorMsg = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
            return Result.fail(errorMsg);
        }
        if (Objects.equals(dto.getPayMethod(), PayMethodConstant.WECHAT)) { // 微信支付
            return Result.success(wechatPayService.createOrder(dto, request));
        }
        return Result.fail();
    }

    /**
     * 微信支付-支付回调
     * @param request
     * @param response
     * @return
     * @throws WxPayException
     * @throws IOException
     */
    @Operation(summary = "微信支付-支付回调", hidden = true)
    @PostMapping("/wechat/notify")
    public Map<String, String> wechatNotify(HttpServletRequest request, HttpServletResponse response) throws WxPayException, IOException {
        return wechatPayService.notify(request, response);
    }

    /**
     * 支付宝-下单
     * @param dto
     * @param bindingResult
     * @param request
     * @param response
     * @return
     * @throws AlipayApiException
     */
    @Operation(summary = "支付宝-下单")
    @PostMapping("/alipay")
    public String alipay(@Validated @RequestBody PayDTO dto,
                       BindingResult bindingResult, HttpServletRequest request,
                       HttpServletResponse response) throws AlipayApiException {
        if (bindingResult.hasErrors()) {
            String errorMsg = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
            throw new BusinessException(errorMsg);
        }
        return alipayService.pay(dto, request, response);
    }

    /**
     * 支付宝-支付回调
     * @param request
     * @return
     */
    @Operation(summary = "支付宝-支付回调", hidden = true)
    @PostMapping("/alipay/notify")
    public String alipayNotify(HttpServletRequest request) {
        return alipayService.notify(request);
    }
}
